package com.pig4cloud.pigx.contract.mq.listener;

import com.pig4cloud.pigx.contract.constant.ContractMqConstants;
import com.pig4cloud.pigx.contract.dto.ContractPositionNeedAppendMarginEmailDTO;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.util.List;

/**
 * 发送需要追加保证金邮件
 *
 * @since 2022/9/17 20:51
 */
@Slf4j
@Component
@AllArgsConstructor
public class EmailMarginCheckListener {

	private final ContractUtils contractUtils;

	/**
	 * 监听 contract.email.margin.check 队列，开始发送需要追加保证金邮件
	 *
	 * @param dtoList dtoList
	 * @since 2022/9/16 16:00
	 */
	@RabbitListener(bindings = {
			@QueueBinding(
					value = @Queue(value = ContractMqConstants.QUEUE_FANOUT_CONTRACT_EMAIL_MARGIN_CHECK),
					exchange = @Exchange(value = ContractMqConstants.EXCHANGE_FANOUT_CONTRACT_EMAIL_MARGIN_CHECK, type = ExchangeTypes.FANOUT)
			)})
	public void run(List<ContractPositionNeedAppendMarginEmailDTO> dtoList) {

		dtoList.forEach(dto -> {
			try {
				contractUtils.sendNeedAppendMarginEmail(dto);
			} catch (MessagingException ignore) {
			}
		});

	}
}
